package com.wkedu.recommend.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wkedu.recommend.dao.entity.ProductOrder;
import com.wkedu.recommend.dao.entity.User;
import com.wkedu.recommend.dao.mapper.ProductOrderMapper;
import com.wkedu.recommend.dao.mapper.ProductTableMapper;
import com.wkedu.recommend.dao.mapper.UserMapper;
import com.wkedu.recommend.entity.ProductTable;
import com.wkedu.recommend.recommender.RecommenderS;
import com.wkedu.recommend.service.MemberUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 港港
 * @version 1.0
 * @date 2021/8/29 下午4:51
 */
@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserMapper userMapper;


    @Resource
    private MemberUserService memberUserService;

    @Resource
    private ProductOrderMapper productOrderMapper;

    @Resource
    private ProductTableMapper productTableMapper;

    @GetMapping("/login")
    public List<ProductTable> userLogin(@RequestParam String username,@RequestParam String password){
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("username",username);
        User user = userMapper.selectOne(userQueryWrapper);
        if (username.equals(user.getUsername()) && password.equals(user.getPassword())) {
            int memberUserId = memberUserService.selectIdByName(user.getName());
            Integer integer = new Integer(memberUserId);
            RecommenderS recommenderS = new RecommenderS();
            List<ProductTable> recommendList = recommenderS.login(integer);
            return recommendList;
        }else {
            return new ArrayList<ProductTable>();
        }
    }

    @GetMapping("/selectOrder")
    public List<com.wkedu.recommend.dao.entity.ProductTable> selectUserOrder(@RequestParam String name){
        int userId = memberUserService.selectIdByName(name);
        QueryWrapper<ProductOrder> productOrderQueryWrapper = new QueryWrapper<>();
        productOrderQueryWrapper.eq("USER_ID",userId);
        List<ProductOrder> productOrderList = productOrderMapper.selectList(productOrderQueryWrapper);
        List<Integer> collect = productOrderList.stream().map(i -> i.getProductId()).collect(Collectors.toList());
        List<com.wkedu.recommend.dao.entity.ProductTable> productTableList = productTableMapper.selectBatchIds(collect);
        return productTableList;

    }
}
